function make_move(xval, yval) {
	if (real_move_num != move_num) {
		go_to_move(real_move_num);
		alert('The game was not being viewed in it\'s current state.\nThis has been fixed.\n\nPlease try your move again.');
		return false;
	}

	document.move.x.value = xval;
	document.move.y.value = yval;
	document.move.submit();
	return true;
}

function loadgame(id) {
	document.playform.game_id.value = id;
	document.playform.submit();
}

function viewgame(id) {
	document.viewform.game_id.value = id;
	document.viewform.spec.value = 's';
	document.viewform.submit();
}

function go_to_move(move) {
	my_alert('===============================================');
	my_alert('GO_TO_MOVE');
	var status = true;

	if (move == move_num) {
		my_alert('move num the same, skipping');
		return false;
	}
	else if (move < move_num) {
		while ((move < move_num) && status) {
			status = prev_move();
		}
	}
	else if (move > move_num) {
		while ((move > move_num) && status) {
			status = next_move();
		}
	}

	return true;
}

function next_move()
{
	my_alert('===============================================');
	my_alert('NEXT_MOVE');
	my_alert('move_num = ' + move_num);
	if (move_num == moves.length - 1)
	{
		my_alert('QUITTING');
		return false;
	}
	else
	{
		my_alert('doing move ' + move_num);
		do_move(move_num + 1);
		my_alert('incrementing move_num');
		++move_num;
		my_alert('move_num = ' + move_num);
		return true;
	}
}

function prev_move()
{
	my_alert('===============================================');
	my_alert('PREV_MOVE');
	my_alert('move_num = ' + move_num);
	if (move_num == 0)
	{
		my_alert('QUITTING');
		return false;
	}
	else
	{
		my_alert('undoing move ' + move_num);
		do_move(move_num);
		my_alert('decrementing move_num');
		--move_num;
		my_alert('move_num = ' + move_num);
		return true;
	}
}

function do_move(this_move_num)
{
	my_alert('DO_MOVE');
	my_alert('this_move_num = ' + this_move_num);
	my_alert('moves = ' + moves);

	// read the move
	var move = moves[this_move_num];
	my_alert('move_num = ' + move_num);
	my_alert('move = ' + move);

	// if we are going backwards
	if (this_move_num <= move_num)
	{
		// undo the move
		var move_string = 'dummy text';
		var move_remainder = move;
		var color, pos, square;
		while (move_remainder != '')
		{
			move_string = move_remainder.slice(0, 3);
			move_remainder = move_remainder.slice(3);

			color = move_string.slice(0, 1);
			pos = move_string.slice(1);

			my_alert('move_string = ' + move_string);
			my_alert('move_remainder = ' + move_remainder);
			my_alert('color = ' + color);
			my_alert('pos = ' + pos);

			if (('zz' == pos) || ('xx' == pos))
			{
				// do nothing, its a resign or a pass
				continue;
			}

			// remove the image from the given square
			if (('b' == color) || ('w' == color))
			{
				square = getObject('s' + pos);
				my_alert('removing stone');
				my_alert('square = ' + square);
				square.innerHTML = '';
			}
			else if ('e' == color) // replace a black stone
			{
				square = getObject('s' + pos);
				my_alert('adding black stone');
				my_alert('square = ' + square);
				square.innerHTML = '<img src="image/b.gif" alt="X" />';
			}
			else if ('r' == color) // replace a white stone
			{
				square = getObject('s' + pos);
				my_alert('adding white stone');
				my_alert('square = ' + square);
				square.innerHTML = '<img src="image/w.gif" alt="O" />';
			}
		}

		// now find out the previous moved stone and highlight it
		var cur_stone = moves[move_num - 1].slice(1, 3);
		my_alert('cur_stone = ' + cur_stone);
		if (('zz' != cur_stone) && ('xx' != cur_stone))
		{
			unmoved_square = getObject('s' + cur_stone).getElementsByTagName('img')[0];
			unmoved_square.src = unmoved_square.src.replace('\.gif', 'm.gif');
		}

		// now change the highlight from the current move to the move we moved to
		unhighlight = getObject('h' + move_num);
		my_alert(unhighlight.className);
		my_alert(unhighlight.className.slice(0, 2));
		unhighlight.className = unhighlight.className.slice(0, 2);
		// keep the coloring

		highlight = getObject('h' + (move_num - 1));
		highlight.className += ' until';
	}
	else // we are moving forward
	{
		// redo the move
		var move_string = 'dummy text';
		var move_remainder = move;
		var color, pos, square;
		while (move_remainder != '')
		{
			move_string = move_remainder.slice(0, 3);
			move_remainder = move_remainder.slice(3);

			color = move_string.slice(0, 1);
			pos = move_string.slice(1);

			my_alert('move_string = ' + move_string);
			my_alert('move_remainder = ' + move_remainder);
			my_alert('color = ' + color);
			my_alert('pos = ' + pos);

			if (('zz' == pos) || ('xx' == pos))
			{
				// do nothing, its a resign or a pass
				continue;
			}

			// remove the image from the given square
			if (('e' == color) || ('r' == color))
			{
				square = getObject('s' + pos);
				my_alert('removing stone');
				my_alert('square = ' + square);
				square.innerHTML = '';
			}
			else if ('b' == color) // add the black stone
			{
				square = getObject('s' + pos);
				my_alert('adding black stone');
				my_alert('square = ' + square);
				square.innerHTML = '<img src="image/bm.gif" alt="X" />';
			}
			else if ('w' == color) // add the white stone
			{
				square = getObject('s' + pos);
				my_alert('adding white stone');
				my_alert('square = ' + square);
				square.innerHTML = '<img src="image/wm.gif" alt="O" />';
			}
		}

		// now find out the previous moved stone and un-highlight it
		my_alert('move_num = ' + move_num);
		var prev_stone = moves[move_num].slice(1, 3);
		my_alert('prev_stone = ' + prev_stone);
		if (('zz' != prev_stone) && ('xx' != prev_stone))
		{
			moved_square = getObject('s' + prev_stone).getElementsByTagName('img')[0];
			my_alert(moved_square);

			// make sure the moved stone was not removed in the current move
			if (undefined != moved_square)
			{
				moved_square.src = moved_square.src.replace('m\.gif', '.gif');
			}
		}

		// now change the highlight from the current move to the move we moved to
		unhighlight = getObject('h' + move_num);
		my_alert(unhighlight.className);
		my_alert(unhighlight.className.slice(0, 2));
		unhighlight.className = unhighlight.className.slice(0, 2);
		// keep the coloring

		highlight = getObject('h' + (move_num + 1));
		highlight.className += ' until';
	}
}


function my_alert(thingy)
{
	if (false)
	{
		document.getElementById('alerts').innerHTML += thingy + '<br />\n';
		//alert(thingy);
	}
}


function getObject(obj) {
	if (document.getElementById) {  // Mozilla, FireFox, Explorer 5+, Opera 5+, Konqueror, Safari, iCab, Ice, OmniWeb 4.5
		if (typeof obj == 'string') {
			if (document.getElementById(obj)) {
				return document.getElementById(obj);
			} else {
				return document.getElementsByName(obj)[0];
			}
		} else {
			return obj.style;
		}
	}
	if (document.all) {	   // Explorer 4+, Opera 6+, iCab, Ice, Omniweb 4.2-
		if (typeof obj == 'string') {
			return document.all(obj);
		} else {
			return obj.style;
		}
	}
	if (document.layers) {	  // Netscape 4, Ice, Escape, Omniweb 4.2-
		if (typeof obj == 'string') {
			return document.layers(obj);
		} else {
			return obj.style;
		}
	}
	alert('Object not found: ' + obj);
	return false;
}